﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RepertorySystem.Models;
using System.Data.Entity.Infrastructure;

namespace RepertorySystem.Controllers
{
    [Authorize]
    public class StorageListsController : Controller
    {
        private StoragesListDbContext db = new StoragesListDbContext();

        // GET: StorageLists
        public ActionResult Index()
        {
            int arryLength = db.StorageLists.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<StorageList> content = db.StorageLists.OrderByDescending(u => u.Book_Number).Skip(0).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.totalPages = totalPages;
            ViewBag.currentPage = 1;

            return View();
        }

        public ActionResult GetNext(int currentPage) {
            int arryLength = db.StorageLists.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<StorageList> content = db.StorageLists.OrderByDescending(u => u.Book_Number).Skip((currentPage - 1) * 5).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.currentPage = currentPage;
            ViewBag.totalPages = totalPages;
            return View("Index");

        }

        public ActionResult FindOne(String book_Name) {
            //查询数据
            this.TempData["book_Name"] = book_Name;
            //跳转
            return RedirectToAction("FindStorageLists", "BookModels", "");

        }

        public ActionResult ReturnOne() {
            BookModels book = new BookModels();
            book = this.TempData["resultBook"] as BookModels;
            int id = book.ID;

            List<StorageList> accountList = db.StorageLists.Where(a => a.Book_Id == book.ID).ToList();


            ViewBag.content = accountList;
            ViewBag.currentPage = 1;
            ViewBag.totalPages = 1;


            return View("Index");
        }

        public ActionResult ReturnNoFind() {

            ViewBag.content = null;
            ViewBag.currentPage = 0;
            ViewBag.totalPages = 0;
            return View("Index");
        }


        // GET: StorageLists/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            StorageList storageList = db.StorageLists.Find(id);
            if (storageList == null)
            {
                return HttpNotFound();
            }
            return View(storageList);
        }

        // GET: StorageLists/Create
        public ActionResult Create()
        {
            if(this.TempData != null) {
                StorageList storageLists = this.TempData["storageList"] as StorageList;
                //StorageList storageList = new StorageList();
                //storageList.Book_Id = storageLists.Book_Id;
                //查询到数据
                var sto = db.StorageLists.Where(u => u.Book_Id == storageLists.Book_Id).FirstOrDefault();
                //如果存在则是修改
                if(sto != null) {
                    //修改属性
                    sto.Book_Number = sto.Book_Number + storageLists.Book_Number;
                    //如果数量小于等于一，直接删除
                    if(sto.Book_Number <= 0) {
                        db.StorageLists.Remove(sto);
                        db.SaveChanges();
                    }
                    //保存修改
                    db.SaveChanges();
                } else {
                    StorageList storageList = new StorageList();

                    storageList.Book_Id = storageLists.Book_Id;
                    storageList.Book_Number = storageLists.Book_Number;

                    db.StorageLists.Add(storageList);
                    db.SaveChanges();
                }

                return RedirectToAction("Index","AccountLists",null);
            }
            return View();
        }

        // POST: StorageLists/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Book_Id,Book_Number")] StorageList storageList)
        {
            if (ModelState.IsValid)
            {
                db.StorageLists.Add(storageList);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(storageList);
        }


        // GET: StorageLists/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            StorageList storageList = db.StorageLists.Find(id);
            if (storageList == null)
            {
                return HttpNotFound();
            }
            return View(storageList);
        }

        // POST: StorageLists/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Book_Id,Book_Number")] StorageList storageList)
        {
            if (ModelState.IsValid)
            {
                db.Entry(storageList).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(storageList);
        }

        // GET: StorageLists/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            StorageList storageList = db.StorageLists.Find(id);
            if (storageList == null)
            {
                return HttpNotFound();
            }
            return View(storageList);
        }

        // POST: StorageLists/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            StorageList storageList = db.StorageLists.Find(id);
            db.StorageLists.Remove(storageList);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
